package guda.stark.biz.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import guda.stark.biz.ExtRuleBiz;
import guda.stark.dal.dao.ExtRuleDAO;
import guda.stark.dal.dataobject.ExtRuleDO;
import guda.grape.autogen.common.page.BaseQuery;
import guda.grape.autogen.common.page.BizResult;

public class ExtRuleBizImpl implements ExtRuleBiz{

    private final static Logger logger = LoggerFactory.getLogger(ExtRuleBizImpl.class);

    @Autowired
    private ExtRuleDAO extRuleDAO;

    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            ExtRuleDO extRuleDO = extRuleDAO.getExtRuleById(id);
            bizResult.data.put("extRuleDO", extRuleDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query ExtRule error",e);
        }
        return bizResult;
}

    public BizResult list(BaseQuery baseQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = extRuleDAO.countForPage(baseQuery);
            baseQuery.setTotalCount(totalCount);
            List<ExtRuleDO> extRuleList = extRuleDAO.selectForPage(baseQuery);
            bizResult.data.put("extRuleList", extRuleList);
            bizResult.data.put("query", baseQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view ExtRule list error", e);
        }
            return bizResult;
     }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            extRuleDAO.delExtRuleById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete extRule error", e);
        }
        return bizResult;
    }

    public BizResult create(ExtRuleDO extRuleDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = extRuleDAO.insert(extRuleDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create ExtRule error", e);
        }
        return bizResult;
    }

    @Override
    public BizResult update(ExtRuleDO extRuleDO) {
        BizResult bizResult = new BizResult();
        try {
            extRuleDAO.updateByIdSelective(extRuleDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update ExtRule error", e);
        }
        return bizResult;
    }


}
